<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加手机配置</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="手机名称" prop="phoneIds">
        <el-select v-model="ruleForm.phoneIds" multiple placeholder="请选择">
          <el-option
              v-for="item in phoneListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              @click.native="add(item.id)">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机简介" prop="disposition">
        <el-input v-model="ruleForm.disposition"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="库存" prop="stock">
        <el-input v-model="ruleForm.stock"></el-input>
      </el-form-item>
      <el-form-item label="手机版本" prop="version">
        <el-input v-model="ruleForm.version"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      phoneListOptions :[],
      ruleForm: {
        disposition: '',
        phoneIds: [],
        phoneId:"",
        price: '',
        stock: '',
        version: '',
      },
      rules: {
        version: [
          {required: true, message: '请输入手机版本', trigger: 'blur'},
          {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
        ],
        phoneId: [
          {required: true, message: '请输入手机Id', trigger: 'blur'},
        ],
        disposition: [
          {required: true, message: '请输入品牌简介', trigger: 'blur'},
          {min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur'}
        ],
        price: [
          {required: true, message: '请输入手机价格', trigger: 'blur'},
        ],
        stock: [
          {required: true, message: '请输入手机库存', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    add(id){
      this.ruleForm.phoneId = id;
      console.log(id)
    },
    loadDetailList() {
      console.log('准备从服务器端加载【手机配置列表】……');
      let url = 'http://localhost:9081/phones';
      console.log('请求路径：' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url)
          .then((response) => {
            let responseBody = response.data;
            console.log('接收到服务器端响应的结果：');
            console.log(responseBody);
            this.phoneListOptions = responseBody.data;
          });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9081/details/add-new';
          console.log('尝试添加品牌……');
          console.log('请求路径为：' + url);
          console.log('请求参数为：' + this.ruleForm);
          console.log(this.ruleForm);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('将ruleForm对象转换为FormData：');
          console.log(formData);
          this.axios.post(url, formData).then((response) => {
            console.log('服务器端响应的结果：' + response);
            console.log(response);
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              console.log('添加手机详情成功');
              this.$message({
                message: '添加手机详情成功！',
                type: 'success'
              });
            } else {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    console.log('mounted');
    this.loadDetailList();
  }
}
</script>

<style scoped>

</style>